﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class LabService
    {
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static int Add(Model.Lab model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Lab(");
            strSql.Append("LabName,LabNO,LabLevel,location,category,area,useDate,telephone,summary,openStudent,openTeacher)");
            strSql.Append(" values (");
            strSql.Append("@LabName,@LabNO,@LabLevel,@location,@category,@area,@useDate,@telephone,@summary,@openStudent,@openTeacher)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@LabName", SqlDbType.VarChar,50),
					new SqlParameter("@LabNO", SqlDbType.VarChar,20),
					new SqlParameter("@LabLevel", SqlDbType.Int,4),
					new SqlParameter("@location", SqlDbType.Int,4),
					new SqlParameter("@category", SqlDbType.Int,4),
					new SqlParameter("@area", SqlDbType.Int,4),
					new SqlParameter("@useDate", SqlDbType.DateTime),
					new SqlParameter("@telephone", SqlDbType.VarChar,50),
					new SqlParameter("@summary", SqlDbType.VarChar,200),
					new SqlParameter("@openStudent", SqlDbType.Bit,1),
					new SqlParameter("@openTeacher", SqlDbType.Bit,1)};
            parameters[0].Value = model.LabName;
            parameters[1].Value = model.LabNO;
            parameters[2].Value = model.LabLevel;
            parameters[3].Value = model.location;
            parameters[4].Value = model.category;
            parameters[5].Value = model.area;
            parameters[6].Value = model.useDate;
            parameters[7].Value = model.telephone;
            parameters[8].Value = model.summary;
            parameters[9].Value = model.openStudent;
            parameters[10].Value = model.openTeacher;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        // 根据条件列出实验室
        public static List<Model.Lab> GetLabList(string strWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT * FROM Lab");
            if (strWhere.Trim() != "")
            {
                sql.Append(" where " + strWhere);
            }
            SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql.ToString());
            List<Model.Lab> list = new List<Lab>();
            if (dr.HasRows)
            {
                Lab l;
                while (dr.Read())
                {
                    l = new Lab();
                    l.LabID = int.Parse(dr["LabID"].ToString());
                    l.LabName = dr["LabName"].ToString();
                    l.LabNO = dr["LabNO"].ToString();
                    l.LabLevel = int.Parse(dr["LabLevel"].ToString());
                    l.location = int.Parse(dr["location"].ToString());
                    l.category = int.Parse(dr["category"].ToString());
                    l.area = int.Parse(dr["area"].ToString());
                    l.useDate = Convert.ToDateTime(dr["useDate"]);
                    l.telephone = dr["telephone"].ToString();
                    l.summary = dr["summary"].ToString();
                    l.openStudent = Convert.ToBoolean(dr["openStudent"]);
                    l.openTeacher = Convert.ToBoolean(dr["openTeacher"]);
                    l.locationValue = ParameterService.SelectOne(l.location);
                    l.categoryValue = ParameterService.SelectOne(l.category);
                    l.levelValue = ParameterService.SelectOne(l.LabLevel);
                    list.Add(l);
                }
                dr.Close();
            }
            dr.Close();
            return list;
        }

        //根据ID查询一条实验室
        public static Lab SelectLabByID(int id)
        {
            string sql = string.Format("SELECT * FROM Lab WHERE LabID = {0}", id);
            SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql);
            Model.Lab l = new Lab();
            if (dr.HasRows)
            {
                dr.Read();
                l.LabID = int.Parse(dr["LabID"].ToString());
                l.LabName = dr["LabName"].ToString();
                l.LabNO = dr["LabNO"].ToString();
                l.LabLevel = int.Parse(dr["LabLevel"].ToString());
                l.location = int.Parse(dr["location"].ToString());
                l.category = int.Parse(dr["category"].ToString());
                l.area = int.Parse(dr["area"].ToString());
                l.useDate = Convert.ToDateTime(dr["useDate"]);
                l.telephone = dr["telephone"].ToString();
                l.summary = dr["summary"].ToString();
                l.openStudent = Convert.ToBoolean(dr["openStudent"]);
                l.openTeacher = Convert.ToBoolean(dr["openTeacher"]);
                l.locationValue = ParameterService.SelectOne(l.location);
                l.categoryValue = ParameterService.SelectOne(l.category);
                l.levelValue = ParameterService.SelectOne(l.LabLevel);
                dr.Close();
            }
            dr.Close();
            return l;
        }

        //根据ID更新一条实验室
        public static bool Update(Model.Lab model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Lab set ");
            strSql.Append("LabName=@LabName,");
            strSql.Append("LabNO=@LabNO,");
            strSql.Append("LabLevel=@LabLevel,");
            strSql.Append("location=@location,");
            strSql.Append("category=@category,");
            strSql.Append("area=@area,");
            strSql.Append("useDate=@useDate,");
            strSql.Append("telephone=@telephone,");
            strSql.Append("summary=@summary,");
            strSql.Append("openStudent=@openStudent,");
            strSql.Append("openTeacher=@openTeacher");
            strSql.Append(" where LabID=@LabID");
            SqlParameter[] parameters = {
					new SqlParameter("@LabName", SqlDbType.VarChar,50),
					new SqlParameter("@LabNO", SqlDbType.VarChar,20),
					new SqlParameter("@LabLevel", SqlDbType.Int,4),
					new SqlParameter("@location", SqlDbType.Int,4),
					new SqlParameter("@category", SqlDbType.Int,4),
					new SqlParameter("@area", SqlDbType.Int,4),
					new SqlParameter("@useDate", SqlDbType.DateTime),
					new SqlParameter("@telephone", SqlDbType.VarChar,50),
					new SqlParameter("@summary", SqlDbType.VarChar,200),
					new SqlParameter("@openStudent", SqlDbType.Bit,1),
					new SqlParameter("@openTeacher", SqlDbType.Bit,1),
					new SqlParameter("@LabID", SqlDbType.Int,4)};
            parameters[0].Value = model.LabName;
            parameters[1].Value = model.LabNO;
            parameters[2].Value = model.LabLevel;
            parameters[3].Value = model.location;
            parameters[4].Value = model.category;
            parameters[5].Value = model.area;
            parameters[6].Value = model.useDate;
            parameters[7].Value = model.telephone;
            parameters[8].Value = model.summary;
            parameters[9].Value = model.openStudent;
            parameters[10].Value = model.openTeacher;
            parameters[11].Value = model.LabID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //根据ID删除一条实验室
        public static bool DeleteByID(int LabID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Lab ");
            strSql.Append(" where LabID=@LabID");
            SqlParameter[] parameters = {
					new SqlParameter("@LabID", SqlDbType.Int,4)
			};
            parameters[0].Value = LabID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  Method
    }
}
